<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组名" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入组名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色名" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="命名空间" prop="namespace">
        <el-input
          v-model="queryParams.namespace"
          placeholder="请输入命名空间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kubernetes:group_binding:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kubernetes:group_binding:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kubernetes:group_binding:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="group_bindingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="组ID" align="center" prop="groupId" />
      <el-table-column label="组名" align="center" prop="groupName" />
      <el-table-column label="角色ID" align="center" prop="roleId" />
      <el-table-column label="角色名" align="center" prop="roleName" />
      <el-table-column label="命名空间" align="center" prop="namespace" />
      <el-table-column label="集群权限" align="center" prop="clusterBinding" :formatter="formatCluster" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kubernetes:group_binding:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改K8s组权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="10">用户组</el-col>
          <el-col :span="12">
            <el-select v-model="form.groupId" filterable placeholder="请选择组">
              <el-option
                v-for="item in groupList"
                :key="item.id"
                :label="item.groupName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">角色</el-col>
          <el-col :span="12">
            <el-select v-model="form.roleId" filterable placeholder="请选择角色">
              <el-option
                v-for="item in cluster_role_list"
                :key="item.id"
                :label="item.role"
                :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">命名空间</el-col>
          <el-col :span="12">
            <el-select v-model="form.namespace" filterable placeholder="请选择命名空间">
              <el-option
                v-for="item in namespace_list"
                :key="item.id"
                :label="item.namespace"
                :value="item.namespace">
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">应用到整个集群
            <el-popover
              placement="top-start"
              width="200"
              trigger="hover"
              content="应用到整个集群会使命名空间选项失效！">
              <i class="el-icon-question" slot="reference"></i>
            </el-popover>
          </el-col>
          <el-col :span="12">
            <el-switch
              v-model="cluster_binding"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGroup_binding, getGroup_binding, delGroup_binding, addGroup_binding, updateGroup_binding } from "@/api/kubernetes/group_binding";
import { listGroup} from "@/api/kubernetes/group";
import { listRole} from "@/api/kubernetes/role";
import { listNamespace } from '@/api/kubernetes/namespace'

export default {
  name: "Group_binding",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // K8s组权限表格数据
      group_bindingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 所有的组
      groupList:[],
      group_queryParams: {
        pageNum: 1,
        pageSize: 999,
        groupName: null
      },
      // 所有的集群角色
      cluster_role_list:[],
      cluster_role_queryParams: {
        pageNum: 1,
        pageSize: 999,
        role: null
      },
      namespace_list:[],
      namespace_queryParams: {
        pageNum: 1,
        pageSize: 999,
        role: null
      },
      cluster_binding:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        groupName: null,
        roleId: null,
        roleName: null,
        namespace: null,
        clusterBinding: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupId: [
          { required: true, message: "组ID不能为空", trigger: "blur" }
        ],
        groupName: [
          { required: true, message: "组名不能为空", trigger: "blur" }
        ],
        roleId: [
          { required: true, message: "角色ID不能为空", trigger: "blur" }
        ],
        roleName: [
          { required: true, message: "角色名不能为空", trigger: "blur" }
        ],
        namespace: [
          { required: true, message: "命名空间不能为空", trigger: "blur" }
        ],
        clusterBinding: [
          { required: true, message: "集群权限不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询K8s组权限列表 */
    getList() {
      this.loading = true;
      listGroup_binding(this.queryParams).then(response => {
        this.group_bindingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // 查询组
      listGroup(this.group_queryParams).then(response=>{
        this.groupList = response.rows;
      })
      // 查询ns
      listNamespace(this.namespace_queryParams).then(response=>{
        this.namespace_list = response.rows;
      })
      // 查询角色
      listRole(this.cluster_role_queryParams).then(response=>{
        this.cluster_role_list = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        groupId: null,
        groupName: null,
        roleId: null,
        roleName: null,
        namespace: null,
        clusterBinding: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加K8s组权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGroup_binding(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改K8s组权限";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.clusterBinding = this.cluster_binding == true ? 1 : 0
      if (this.cluster_binding!=true&&this.form.namespace==null){
        this.$modal.msgError("请选择namespace！");
        return
      }
      if (this.form.groupId==null||this.form.roleId==null){
        this.$modal.msgError("请选择对应参数！");
        return
      }
      this.form.groupName = this.groupList.find(o=>o.id=this.form.groupId).groupName
      this.form.roleName = this.cluster_role_list.find(o=>o.id=this.form.roleId).role
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup_binding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup_binding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除K8s组权限编号为"' + ids + '"的数据项？').then(function() {
        return delGroup_binding(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kubernetes/group_binding/export', {
        ...this.queryParams
      }, `group_binding_${new Date().getTime()}.xlsx`)
    },
    /**  格式化集群状态显示 */
    formatCluster(row){
      return row.clusterBinding === 0 ? "否" : "是";
    }
  }
};
</script>
<style>
  .el-row {
    margin-bottom: 20px;
  &:last-child {
     margin-bottom: 0;
   }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
